var imgurls="";
$(function () {
    imgurls = $('#imgUrls').attr("value");
    layui.use(['upload','layedit'], function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: '/common/sysFile/upload'
            , accept: 'file'
            , multiple: true
            , auto: false
            , bindAction: '#shangchuan'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td id="shangchuan_status">等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            , done: function (res, index, upload) {
                console.log("res"+res)
                if (res.code == 0) { //上传成功
                    if (imgurls!=''){
                      //  imgurls=imgurls+","+res.fileName+",";
                        imgurls=imgurls + res.fileName+",";
                    } else {
                        imgurls=imgurls+""+res.fileName+",";
                    }
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    // tds.eq(3).html(''); //清空操作
                    tds.eq(3).find('.demo-delete').addClass('layui-hide');
                    $('#shangchuan_status').val('上传成功');
                    return delete this.files[index]; //删除文件队列已经上传成功的文件

                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
        layedit = layui.layedit;
        layedit.set({	//设置图片接口
            uploadImage: {
                url: '/common/sysFile/uploadImage', //接口url
                type: 'post'
            }
        });
        index = layedit.build('demo',{
            // tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', 'link', 'unlink', 'face'],
        }); //建立编辑器
    });
})

$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        $('#demo').val(layedit.getContent(index))
        $('#imgUrls').val(imgurls);
        update();
    }
});

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/farm/farmRoom/update",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入名字"
            }
        }
    })
}

var imgArr = []
var imgArr1 = []
$(function () {
    var imgs = $('#imgList').attr("value");
    imgArr1 = imgs.split(',');
    for (var i = 0; i < imgArr1.length; i++) {
        if (imgArr1[i]!=""){
            imgArr.push(imgArr1[i])
        }
    }
})

$("#imgList").on("click", 'span', function() {
    imgurls='';
    var p = $(this).parent().hide();
    var url = $(this).parent().attr("data-id");
    for (var i = 0; i < imgArr.length; i++) {
        if (imgArr[i]==url){
            imgArr.splice(i,1);
        }
    }
    if (imgArr.length>0){
        for (var i = 0; i < imgArr.length; i++) {
            imgurls += imgArr[i]+','
        }
    }else {
        imgurls = '';
    }
})
/*$("#imgList").on("click", 'span', function() {
    var p = $(this).parent().hide();
    var imgs = $('#imgList').attr("value");
    var imgArr = imgs.split(',');
 //   alert(imgArr.length)
    var url = $(this).attr("data-id");
 //   alert(url)
/!*    for (var i = 0; i < imgArr.length; i++) {
        if (imgArr[i]==url){
            imgArr.splice(i,1);
        }
    }*!/
    if (imgArr.length>0){
        if (imgArr.length == 2){
            imgurls = imgurls.replace(url + "," , "")
        } else {
         //   alert("else")
            for (var i = 0; i < imgArr.length; i++) {
           //     alert("imgArr[i]:" + imgArr[i])
               // imgurls += imgArr[i]+','
                imgurls = imgurls.replace(url + "," , "")
            }
        }

    }else {
        imgurls = '';
    }
})*/

function roomPreview(id) {
    var url = "/farm/farmRoom/roomPreview/"+id;
    var title = '房间'
    openPage(url,'预览房间')
    /*var perContent = layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: false,
        area: ['800px', '500px'],
        maxmin: true, //开启最大化最小化按钮
        content: url // iframe的url
    });
    layer.full(perContent);*/
}
